<?php
/* this file will check whether certain product is available in stock */
// load wordpress defaults, make global variables/functions available 
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];
require( $this_file . 'wp-blog-header.php' );
header( 'HTTP/1.1 200 OK' );

// globals
$msg_str = '';
$arr_missing_items = array();
$this_domain = home_url();
$this_domain_arr = explode( '/', $this_domain );
$this_referr = $_SERVER[ 'HTTP_REFERER' ];
$this_referr_arr = explode( '/', $this_referr );

// theme options
$aOptions  = SofaSuppaStore::initOptions( false );
$num_of_mb = $aOptions[ 'num_of_def_boxes' ];
if( $num_of_mb <= 0 ) $num_of_mb = 1;

// get alpha admin email
$owner = get_userdata( 1 );
$adminmail = $owner->user_email;
$arr_adminmail = explode( '@', $adminmail );
$robot_sender = 'noreply@' . $arr_adminmail[ 1 ];

if( $this_domain_arr[ 0 ] == $this_referr_arr[ 0 ] && $this_domain_arr[ 2 ] == $this_referr_arr[ 2 ] ) {
	
	$serial_data = urldecode( $_POST[ 'serialdata' ] );
	$serial_data = stripslashes( strip_tags( $serial_data ) );
	
	if( $serial_data != '' ) {
		
		// update post meta: stock inventory
		$arr_temp_all_basket_items = array();
		$arr_to_merge_products = array();
		$arr_all_ids = array();
		$arr_items_in_basket = explode( '^', $serial_data );
		array_pop( $arr_items_in_basket ); // remove last empty element
		$cnt_aiib = 0;
		$aiib_len = count( $arr_items_in_basket );
		
		if( $aiib_len > 0 ) {
			
			// check for duplicated items in da basket
			$z = 0;
			while( $z < $aiib_len ) {
				$arr_temp = explode( '~', $arr_items_in_basket[ $z ] );
				array_push( $arr_temp_all_basket_items, $arr_temp[ 0 ] . '^' . $arr_temp[ 1 ] ); // must match both product ID and size
				$z ++;
			}
			
			$matches_count = array_count_values( $arr_temp_all_basket_items ); // check for multiple occurences
			
			foreach( $matches_count as $k => $v ) {
				
				if( $v > 1 ) { // any?
					$arr_occurences = array_keys( $arr_temp_all_basket_items, $k ); // where exactly, at which positions...get the key
					
					if( count( $arr_occurences ) > 0 ) {
						$m = 0;
						$cnt_o = count( $arr_occurences );
						$final_product_quantity = 0;
						while( $m < $cnt_o ) {
							$val_temp = $arr_items_in_basket[ $arr_occurences[ $m ] ];
							$arr_val_temp = explode( '~', $val_temp ); // ID, SIZE, QUANTITY, COOKIE
							$final_product_quantity = $final_product_quantity + $arr_val_temp[ 2 ];
							$arr_val_temp[ 2 ] = $final_product_quantity;
							$arr_items_in_basket[ $arr_occurences[ $m ] ] = implode( '~', $arr_val_temp );
							if( $m > 0 ) $arr_items_in_basket[ $arr_occurences[ $m - 1 ] ] = implode( '~', $arr_val_temp ); // backward update
							$m ++;
						}
					}
				}
			}
			
			// make array unique in disregard of duplicated items
			$arr_items_in_basket = array_unique( $arr_items_in_basket );

			// proceed
			while( $cnt_aiib < $aiib_len ) {
			  
				$arr_sub_split_item = explode( '~', $arr_items_in_basket[ $cnt_aiib ] );
				$single_id = intval( $arr_sub_split_item[ 0 ] );
				
				// get all required...
				$arr_sizes = array();
				$arr_stock = array();
				$kk = 0;
				while( $kk < $num_of_mb ) {
					$single_sizes = 'sofa_' . $kk . '_textarea_size';
					$single_stock = 'sofa_' . $kk . '_textarea_stock';
					array_push( $arr_sizes, get_post_meta( $single_id, $single_sizes, true ) );
					array_push( $arr_stock, get_post_meta( $single_id, $single_stock, true ) );
					$kk ++;
				}
				
				// check stock for real...
				$serach_size = trim( $arr_sub_split_item[ 1 ] );
				$pos_in_arr = array_search( (string)$serach_size, $arr_sizes ); // get the key for stock value
				$item_quantity = intval( $arr_sub_split_item[ 2 ] ); // quantity placed in buyer order
				$stock_calc = $arr_stock[ $pos_in_arr ] - $item_quantity; // calculate if stock fits
				if( $stock_calc < 0 ) {
					// can't proceed. gotta notify both user and site admin. User needs to know that his/her order can't be accepted while admin has to be notified about product missing in stock.
					$msg_str .= urlencode( $single_id . '~' . $arr_stock[ $pos_in_arr ] . '~' . $arr_sub_split_item[ 3 ] . '^' );
					array_push( $arr_missing_items, $target_post );
				}
				
				$cnt_aiib ++;
			  
			}
		  
		}
	
	}
	
}

echo $msg_str;
?>